package com.tadpole.music.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.tadpole.music.config.Constant;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.event.WeiXinEvent;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

/**
 * 微信支付回调Activity
 * Created by SunYan
 * on 2020/5/7.
 */
public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {
    private IWXAPI wxAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        wxAPI = WXAPIFactory.createWXAPI(this, Constant.WX_APP_ID);
        wxAPI.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent){
        super.onNewIntent(intent);
        setIntent(intent);
        wxAPI.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {}

    @Override
    public void onResp(BaseResp resp) {
        Log.i("sunyan", "微信支付回调 返回错误码:"+resp.errCode+" 错误名称:"+resp.errStr);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX){//微信支付
            WeiXinEvent weiXin = new WeiXinEvent(3,resp.errCode,"");
            EventBus.getDefault().post(weiXin);
        }
        finish();
    }
}
